package pers.qianyu.month_202102.date_20210207;

/**
 * 665. 非递减数列
 * https://leetcode-cn.com/problems/non-decreasing-array/
 *
 * @author mizzle rain
 * @date 2021-02-07 11:53
 */
public class CheckPossibility {
    public boolean checkPossibility(int[] nums) {
        int cnt = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i - 1] > nums[i]) {
                cnt++;
                if (i - 2 >= 0 && nums[i - 2] > nums[i]) {
                    nums[i] = nums[i - 1];
                } else {
                    nums[i - 1] = nums[i];
                }
                if (cnt >= 2) return false;
            }
        }
        return true;
    }
}
